package com.duowei.kitchenshow.fragment.setting;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.SwitchPreference;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.duowei.kitchenshow.BuildConfig;
import com.duowei.kitchenshow.R;
import com.duowei.kitchenshow.app.MyApp;
import com.duowei.kitchenshow.httputils.Post;
import com.duowei.kitchenshow.utils.SpUtils;
/**
 * A simple {@link Fragment} subclass.
 */
public class OtherSettingFragment extends PreferenceFragment implements
        SharedPreferences.OnSharedPreferenceChangeListener,
        Preference.OnPreferenceClickListener {

    private SharedPreferences mSpf;
    private SwitchPreference mSpfAuto;
    private Context mContext;
    private EditTextPreference mEtCallMessage;
    private ListPreference mListName;
    private ListPreference listCallStyle;
    private PreferenceCategory pfc;

    public OtherSettingFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference_other);
        mContext = MyApp.getContext();
        initPreference();
    }

    private void initPreference() {
        mSpf = getPreferenceScreen().getSharedPreferences();
        boolean auto = mSpf.getBoolean(getString(R.string.autostart), false);
        mSpfAuto = (SwitchPreference) findPreference(getString(R.string.autostart));
        mSpfAuto.setChecked(auto);
        if(auto){
            mSpfAuto.setSummary("开启");
        }else{
            mSpfAuto.setSummary("关闭");
        }

        listCallStyle = (ListPreference) findPreference(getString(R.string.callStyle));
        mEtCallMessage = (EditTextPreference) findPreference(getString(R.string.callMessage));
        mEtCallMessage.setSummary(mSpf.getString(mContext.getString(R.string.callMessage),
                "*号，*号，丝恋喊你过来取餐啦!"));
        mListName = (ListPreference) findPreference(getString(R.string.voiceName));
        mListName.setSummary(mSpf.getString(mContext.getString(R.string.voiceName),"xiaoyan"));
        listCallStyle.setSummary(mSpf.getString(mContext.getString(R.string.callStyle),"padCall"));
        pfc = (PreferenceCategory) findPreference("pfc");

        Preference pVersion = findPreference(getString(R.string.version));
        pVersion.setSummary(BuildConfig.VERSION_NAME);

        mSpf.registerOnSharedPreferenceChangeListener(this);
        pVersion.setOnPreferenceClickListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(key.equals(getString(R.string.autostart))){
            boolean auto = sharedPreferences.getBoolean(getString(R.string.autostart), false);
            SpUtils.put(getString(R.string.autostart),auto);
            if(auto){
                mSpfAuto.setSummary("开启");
            }else{
                mSpfAuto.setSummary("关闭");
            }
        }else if(key.equals(getString(R.string.callMessage))){
            String call = sharedPreferences.getString(getString(R.string.callMessage),
                    "*号，*号，丝恋喊你过来取餐啦!");
            mEtCallMessage.setSummary(call);
            SpUtils.put(getString(R.string.callMessage),call);
        }else if(key.equals(getString(R.string.voiceName))){
            String name = sharedPreferences.getString(getString(R.string.voiceName), "xiaoyan");
            mListName.setSummary(name);
            SpUtils.put(getString(R.string.voiceName),name);
        }else if(key.equals(getString(R.string.callStyle))){
            String call = sharedPreferences.getString(getString(R.string.callStyle), "padCall");
            listCallStyle.setSummary(call);
            SpUtils.put(getString(R.string.callStyle),call);
            if(call.equals("padCall")){
                getPreferenceScreen().addPreference(pfc);
            }else{
                getPreferenceScreen().removePreference(pfc);
            }
        }
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        Post.getInstance().checkVersion(getActivity(),true);
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSpf.unregisterOnSharedPreferenceChangeListener(this);
    }
}
